package com.shuyuansystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shuyuansystem.dao.*;
import com.shuyuansystem.pojo.OrderInfo;
import com.shuyuansystem.pojo.Product;
import com.shuyuansystem.service.ProductService;
import com.shuyuansystem.utils.PageResult;
import com.shuyuansystem.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private merchantProductMapper merchantProductMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public List<Product> getAllProduct() {

        return productMapper.getAllproduct();
    }
//分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        // 开启分页功能
        PageHelper.startPage(currentPage, pageSize);

        // 执行查询
        List<Product> productList = productMapper.findByCondition(queryString);

        // 获取分页信息
        PageInfo<Product> pageInfo = new PageInfo<>(productList);

        // 获取总记录数
        Long total = pageInfo.getTotal();

        // 封装分页结果
        return new PageResult(total, productList);
    }

    @Override
    public String getProductNameById(Integer productId) {
        return  productMapper.getProductNameById(productId);
    }

    @Override
    public BigDecimal getProductPriceById(Integer productId) {
        return productMapper.getProductPriceById(productId);
    }
    @Override
    public List<Product> getProductsByProductIds(List<Integer> productIds) {
        return productMapper.getProductsByProductIds(productIds);
    }
    @Override
    public List<Product> getProductsByProductIdsWithPagination(List<Integer> productIds, int offset, int pageSize) {
        return productMapper.getProductsByProductIdsWithPagination(productIds, offset, pageSize);
    }

    @Override
    public int getTotalCountByProductIds(List<Integer> productIds) {
        return productMapper.getTotalCountByProductIds(productIds);
    }

    @Override
    public Product getProductById(Integer productId) {
        return productMapper.getProductById(productId);
    }

    @Override
    public boolean updateProduct(Product product) {
        int rows = productMapper.updateProduct(product);
        return rows > 0;
    }

    @Transactional
    @Override
    public boolean deleteProduct(Integer productId) {
        try {
            // 先删除 shopping_carts 表中的关联数据
            shoppingCartMapper.deleteByProductId(productId);
            // 再删除 merchant_products 表中的关联数据
            merchantProductMapper.deleteByProductId(productId);
            // 最后删除 products 表中的商品数据
            int rows = productMapper.deleteProduct(productId);
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public boolean addProduct(Product product) {
        int rows = productMapper.addProduct(product);
        return rows > 0;
    }

    @Override
    public Product getByProductId(String productId) {
        return productMapper.getByProductId(productId);
    }
}
